package edu.jamestang.yeb.service.impl;
import java.time.LocalDateTime;

import cn.afterturn.easypoi.excel.entity.ExportParams;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import edu.jamestang.yeb.entity.Employee;
import edu.jamestang.yeb.entity.MailConstants;
import edu.jamestang.yeb.entity.MailLog;
import edu.jamestang.yeb.mapper.EmployeeMapper;
import edu.jamestang.yeb.mapper.MailLogMapper;
import edu.jamestang.yeb.service.EmployeeService;
import edu.jamestang.yeb.util.PageResp;
import edu.jamestang.yeb.util.Resp;
import org.springframework.amqp.rabbit.connection.CorrelationData;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.DecimalFormat;
import java.time.LocalDate;
import java.time.temporal.ChronoUnit;
import java.util.List;
import java.util.Map;
import java.util.UUID;

/**
 * @author JamesTang
 * @date 2021/12/22 11:27
 */

@Service
public class EmployeeServiceImpl extends ServiceImpl<EmployeeMapper, Employee> implements EmployeeService {


    @Autowired
    private EmployeeMapper employeeMapper;

    @Autowired
    private RabbitTemplate rabbitTemplate;

    @Autowired
    private MailLogMapper mailLogMapper;

    @Override
    public PageResp getEmployeeByPage(Integer currentPage, Integer size, Employee employee, LocalDate[] dateScope) {

        Page<Employee> page = new Page<>(currentPage,size);
        IPage<Employee> employeeByPage = employeeMapper.getEmployeeByPage(page, employee, dateScope);

        return new PageResp(employeeByPage.getTotal(), employeeByPage.getRecords());
    }

    @Override
    public Resp maxWorkId() {

        List<Map<String, Object>> maps = baseMapper.selectMaps(new QueryWrapper<Employee>().select("max(work_id)"));
        return Resp.success(null,String.format("%08d",Integer.parseInt(maps.get(0).get("max(work_id)").toString())+1));
    }

    @Override
    public Resp addEmployee(Employee employee) {

        LocalDate beginContract = employee.getBeginDate();
        LocalDate endContract = employee.getEndContract();
        long days = beginContract.until(endContract, ChronoUnit.DAYS);
        DecimalFormat decimalFormat = new DecimalFormat("##.00");
        employee.setContractTerm(Double.parseDouble(decimalFormat.format(days/365.00)));

        if(employeeMapper.insert(employee) == 1){

            Employee emp = employeeMapper.getEmployee(employee.getId()).get(0);

            String msgId =UUID.randomUUID().toString();
            MailLog mailLog = new MailLog();
            mailLog.setMsgId(msgId);
            mailLog.setEid(employee.getId());
            mailLog.setStatus(0);
            mailLog.setRouteKey(MailConstants.MAIL_ROUTING_KEY_NAME);
            mailLog.setExchange(MailConstants.MAIL_EXCHANGE_NAME);
            mailLog.setCount(0);
            mailLog.setTryTime(LocalDateTime.now().plusMinutes(MailConstants.TIME_OUT));
            mailLog.setCreateTime(LocalDateTime.now());
            mailLog.setUpdateTime(LocalDateTime.now());
            mailLogMapper.insert(mailLog);



            rabbitTemplate.convertAndSend(MailConstants.MAIL_EXCHANGE_NAME, MailConstants.MAIL_ROUTING_KEY_NAME, emp, new CorrelationData(msgId));
            return Resp.success("Add successful");
        }
        return Resp.error("Add fail");
    }

    @Override
    public List<Employee> getEmployee(Integer id) {
        return employeeMapper.getEmployee(id);
    }

    @Override
    public PageResp getEmployeeWithSalary(Integer currentPage, Integer size) {

        Page<Employee> page = new Page<>(currentPage,size);
        IPage<Employee> employeeIPage = employeeMapper.getEmployeeWithSalary(page);

        return new PageResp(employeeIPage.getTotal(), employeeIPage.getRecords());
    }
}
